/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： mysite
 * 文件名称： articlelist.js
 * Date：18-9-26 下午4:24
 * Author: guomw
 */

$(function () {
    articleHandler.init();
});
var articleHandler = {
    localData: null,
    templateList: $("#template_list").html(),
    params: {
        keyword: "",
        articleStatus: -1,
        pageIndex: 1
    },
    init: function () {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/article/ajax/articlelist", self.params, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                var data = ret.data;
                self.localData = data.list;
                var appendHtml = '';
                $.each(data.list, function (i, item) {
                    var tempHtml = self.templateList;
                    tempHtml = tempHtml.replace(/{index}/gm, i + 1);
                    tempHtml = tempHtml.replace(/{articleId}/gm, item.articleId);
                    tempHtml = tempHtml.replace(/{ArticleTitle}/gm, item.articleTitle);
                    tempHtml = tempHtml.replace(/{classifyName}/gm, item.classify != null ? item.classify.classifyTitle : '');
                    tempHtml = tempHtml.replace(/{ArticleCover}/gm, item.articleCover);
                    tempHtml = tempHtml.replace(/{PublishTime}/gm, item.articleStatus == 1 ? item.publishTime : '--');
                    tempHtml = tempHtml.replace(/{CreateTime}/gm, item.createTime);
                    tempHtml = tempHtml.replace(/{UpdateTime}/gm, item.updateTime);
                    tempHtml = tempHtml.replace(/{articleStatus}/gm, item.articleStatus == 0 ? '未发布' : item.articleStatus == 1 ? '已发布' : '已下架');
                    appendHtml += tempHtml;
                });
                $("#table-content").html(appendHtml);

                var pageinate = new hotUtil.paging(".pagination", self.params.pageIndex, 20, data.pageCount, data.totalCount, 7);
                pageinate.init(function (p) {
                    goTo(p, function (page) {
                        self.params.pageIndex = page;
                        self.init();
                    });
                });
            }
        });
    },
    getModel: function (dataId) {
        var model = null;
        if (this.localData != null && this.localData.length > 0) {
            $.each(this.localData, function (i, item) {
                if (item.articleId === dataId) {
                    model = item;
                    return false;
                }
            });
        }
        return model;
    },
    edit: function (dataId) {
        var data = this.getModel(dataId);
        if (data != null)
            hotUtil.newTab("article/edit?articleId=" + dataId, "编辑文章[" + data.articleTitle + "]");
        else
            hotUtil.newTab("article/edit?articleId=0", "添加文章");
    },
    search: function () {
        this.params.pageIndex = 1;
        this.params.keyword = $("#keyword").val();
        this.params.articleStatus = $("#sl_status").val();
        this.init();
    },
    searchAll: function () {
        $("#keyword").val("");
        $("#sl_status").val(-1);
        this.params.pageIndex = 1;
        this.params.keyword = "";
        this.params.articleStatus = -1;
        this.init();
    },
    updateStatus: function (articleId, status) {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/article/ajax/updateStatus", {
            articleId: articleId,
            status: status
        }, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                self.init();
                swal("设置成功", "", "success");
            }
            else {
                swal("设置失败", "", "error");
            }
        })
    }
}